<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Video extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('videos');
		$this->load->model('courses');
		$this->smarty->assign("menu_id", 1);
	}

	/**
	** 网课-视频列表
	*/
	public function index($order = 0,$cate = 0,$subject = 0,$page = 1){
		

		$limit = 0;
		$offset = 9;
		if($page > 0){
			$limit = ($page - 1) * $offset;
		}
		$info = array('order' => $order , 'cate' => $cate ,'subject' => $subject);
		//课程分类
		$course_cate = $this->videos->get_video_cate();
		

		$subject_cate = $this->videos->get_course_cate($cate);



		$list = $this->courses->get_course_list($order,$cate,$subject,$offset,$limit);
		$page_config['uri_segment'] = 6;
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$page_config['base_url'] = '/video/index/'.$order.'/'.$cate.'/'.$subject;
		$page_config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		//获取一条直播
		$this->load->model('lives');
		$zhibo = $this->lives->get_last_zhibo();
		if($zhibo){
			$time = time();
			if($zhibo['start_time'] < $time && $time < $zhibo['end_time']){
				$zhibo['live_status'] = 2;
			}
			if($zhibo['start_time'] > $time){
				$zhibo['live_status'] = 1;
			}
			if($zhibo['end_time'] < $time){
				$zhibo['live_status'] = 3;
			}
			$this->smarty->assign("zhibo", $zhibo);
		}


		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("course_cate", $course_cate);
		$this->smarty->assign("subject", $subject_cate);
		$this->smarty->assign("info", $info);
		$this->smarty->assign("list", $list);
		$this->smarty->assign("menutag", "course");
		$this->smarty->display("course/video_list.html");

	}

	/**
	* 视频课详情
	*/
	public function detail($id = 0){
		$info = $this->videos->get_course_info($id);
		if(empty($info)){
			echo "该课程不存在！！";exit;
		}
		$video_info = get_field_by_where("video", "image_url,video_url", "id='{$info['vid']}'");
		$this->smarty->assign("video_info", $video_info);
		//var_dump($logged_in);exit;
		$chapter_info = $this->videos->get_video_info($id);
		foreach ($chapter_info as $key => $value) {
			$chapter_info[$this->floatohz($key+1)] = $value;
			unset($chapter_info[$key]);
		}
		/*$order_buy = $this->courses->check_is_buy($id,1);

		if(!empty($order_buy)){
			$effective_time = $order_buy['add_time'] + $order_buy['effective_time']*24*60*60;
			$time = time();
			if($time < $effective_time){
				$is_buy = 1;
			}else{
				$is_buy = 2;
			}
		}else{
			$is_buy = 0;
		}*/
		$is_buy = 0;
		if($this->user_data['id']){
			$crad_info = check_user_learn_card($this->user_data['id'], $id, 1);
			$order_buy = check_effective_time($this->user_data['id'], $id, 1);
			if($crad_info || $order_buy || ($info['is_free'] == 1 && get_vip_endtime($this->user_data['id']) > time())){
				$is_buy = 1;
			}
		}
		
		$school_course_count = $this->videos->get_school_course_count($info['sid']);
		$xuesheng_count = $this->videos->get_school_xuesheng_count($info['sid']);
		$info['course_count'] = $school_course_count;
		$info['xuesheng_count'] = $xuesheng_count;
		$this->smarty->assign("info", $info);
		$this->smarty->assign("is_buy", $is_buy);
		$this->smarty->assign("chapter", $chapter_info);
		$this->smarty->assign("menutag", "course");
		$this->smarty->display("course/video_detail.html");
	}

	public function video_comment_list($course_id = 0, $star = 0, $limit = 0){

		$offset = 5;
		$list = $this->videos->get_comment_list($course_id,$star,$limit,$offset);

		$page_config['uri_segment'] = 5;
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$page_config['base_url'] = '/video/video_comment_list/'.$course_id.'/'.$star;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course/comment_list.html");
	}


  	public function play($id = 0,$chapter_id = 0){
	  	//$value='10';  
		//print $this->floatohz($value);exit;  
		//echo "<br>";  
		/*$order_buy = $this->courses->check_is_buy($id,1);

		if(!empty($order_buy)){
			$effective_time = $order_buy['add_time'] + $order_buy['effective_time']*24*60*60;
			$time = time();
			if($time < $effective_time){
				$is_buy = 1;
			}else{
				echo "购买已经过期";exit;
				$is_buy = 2;
			}
		}else{
			echo "请先购买课程";exit;
			$is_buy = 0;
		}*/
		$info = $this->videos->get_course_info($id);
		if(empty($info)){
			echo "该课程不存在！！";exit;
		}
		$this->smarty->assign("is_free", $info['is_free']);
		$is_buy = 0;
		if($this->user_data['id']){
			$crad_info = check_user_learn_card($this->user_data['id'], $id, 1);
			$order_buy = check_effective_time($this->user_data['id'], $id, 1);
			if($crad_info || $order_buy || ($info['is_free'] == 1 && get_vip_endtime($this->user_data['id']) > time())){
				$is_buy = 1;
			}else{
				show_error_message("/video/detail/".$id, "请先购买课程");exit;
			}
		}else{
			show_error_message("/login", "请先登录");exit;
		}

		
	  	$info = $this->videos->get_course_info($id);
	  	$info['effective_day'] = get_effective_time($this->user_data['id'], $id, 1);
	  	if(!$info){
	  		show_error_message("/video", "课程不存在");
	  		exit;
	  	}
	  	if($chapter_id > 0){
	  		$chapter_info = $this->videos->get_course_chapter_info($chapter_id);
			if(!$chapter_info || $chapter_info['course_id'] != $id){
				show_error_message("/video", "课程不存在");
	  			exit;
			}
	  	}
	  	$chapter_info = $this->videos->get_video_info($id);
	  	$count = 0;
	  	foreach ($chapter_info as $key => $value) {
			$chapter_info[$this->floatohz($key+1)] = $value;
			unset($chapter_info[$key]);
		}
		//var_dump($chapter_info);exit;
		foreach ($chapter_info as $key => $value) {
			if(isset($value['jie']) && is_array($value['jie'])){ //用is_array是否为数组
		     	$count += count($value['jie']);
		     	foreach ($value['jie'] as $k => $v) {
		     		$ranking[] = $v;
		     	}
		  	}
		}
		//var
		$count_ranking  = 0;
		if($chapter_id == 0){
			$count_ranking = 1;
		}else{
			foreach ($ranking as $key => $value) {
				if($value['id'] == $chapter_id){
					$count_ranking = $key+1;
				}
			}	
		}
		
		//echo $count_ranking;
		//var_dump($ranking);
		//echo $count;
		if($count == 0){
			$count = 1;
		}
		$progress = (int)($count_ranking/$count*100);
		$remaining = $count - $count_ranking;
	  	$video = $this->videos->get_first_video_url($id,$chapter_id);
	  	if($video){
	  		$this->db->where(array('id'=>$video['vid']));
	  		$this->db->set('click_num','click_num + 1',FALSE);
	  		$this->db->update('video');
	  	}
	  	$school_course_count = $this->videos->get_school_course_count($info['sid']);
		$xuesheng_count = $this->videos->get_school_xuesheng_count($info['sid']);
		$info['course_count'] = $school_course_count;
		$info['xuesheng_count'] = $xuesheng_count;
	  	//print_r($chapter_info);exit;
	  	$this->smarty->assign("chapter", $chapter_info);
	  	$this->smarty->assign("menutag", "course");
	  	$this->smarty->assign("video", $video);
	  	$this->smarty->assign("progress", $progress);
	  	$this->smarty->assign("remaining", $remaining);
	  	$this->smarty->assign("info", $info);
	    $this->smarty->display("course/video_play.html");
	}

	public function free_play($chapter_id = 0){
		$code = 0;
		$msg = '';
		if($chapter_id > 0){
			$chapter_info = $this->videos->get_course_chapter_info($chapter_id);
			if($chapter_info){
				if($chapter_info['vid'] > 0 && $chapter_info['is_free'] == 1){
					$code = 1;
					$msg = "即将播放";
					$this->smarty->assign("video", $chapter_info);
				}
			}else{
				$code = 0;
				$msg = "课程不存在";
			}
		}else{
			$code = 0;
			$msg = "请选择课程";
		}
		if($code == 0){
			show_error_message("/", $msg);
			exit;
		}
	    $this->smarty->display("course/free_play.html");
	}


  	function floatohz($value){  
	    $result='';  
		$v_a=array('','','','','十',',百','千','万','十','百','千','亿');  
	    $v_b=array('零','一','二','三','四','五','六','七','八','九','十');  
	    $v_c=array();  
	    $value=(string)$value;  
	    $value=sprintf("%0.2f",$value);  
	    $len=strlen($value);  
	    for($i=$len;$i>=0;$i--){  
	       $val = @$value[$i]; //$VALUE 不是数组  
	       if($val!='.'){  
	           if($val!='0')  
	              @$v_c[]=$v_b[$val].$v_a[$len-$i-1];  
	       }  
	    }  
	    $v_c=array_reverse($v_c);  
	    foreach($v_c as $val){  
	       $result.=$val;  
	    }  
	    unset($v_a);unset($v_b);unset($v_c);  
	    return $result;  
	}   


}
